package cn.boysoft.core.handler.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import cn.boysoft.core.exception.JEEException;
import cn.boysoft.core.handler.BusinessHandler;
import cn.boysoft.core.model.message.Request;
import cn.boysoft.core.server.MainServer;
import cn.boysoft.core.server.ServiceInfo;
/**
 * @author 付厚俊
 * CreateTime 2013-4-4 下午5:22:47
 */

@Service(value="businessHandler")
public class DefaultBusinessHandler implements BusinessHandler {
	private final static Logger LOGGER = LoggerFactory.getLogger(DefaultBusinessHandler.class);
	@Override
		public Object execute(Request request) {
			String commandId = request.getServiceId();
			ServiceInfo serviceInfo =  MainServer.getOnlyReadServiceInfo(commandId);
			if(serviceInfo != null){
				Object service = MainServer.getBean(serviceInfo.getServiceClassId());
				if(serviceInfo.getDataClazz() == null){
					return serviceInfo.invoke(service);
				}else{
					return serviceInfo.invoke(service,request.getData());
				}	
			}else{
				 LOGGER.error("Service "+commandId+" not find");
				 throw new JEEException(JEEException.NOT_FIND_SERVICE);
			}
		}
}
